/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.transport.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.argot.network.TypeEndPoint;
import com.argot.network.TypeLink;

import com.colony.transport.Server;

import com.colony.concurrent.ThreadActivator;
import com.colony.mime.MimeInputSource;
import com.colony.mime.MimeOutputSink;

public class HttpServer
extends Server
{

	public HttpServer( TypeLink back, ThreadActivator pool )
	{
		super( back, pool );
	}
	
	public void processMessage( HttpServletRequest request, HttpServletResponse response )
	throws IOException
	{
		InputStream input = request.getInputStream();
		OutputStream output = response.getOutputStream();
       	response.setContentType("binary/colony");
       	
		MimeInputSource source = new MimeInputSource( input );
		MimeOutputSink sink = new MimeOutputSink( output );
		
		HttpServerRequest serverRequest = new HttpServerRequest( source, sink, output, request, response );
		
		processMessage( serverRequest );
	}

	protected void processFinish(TypeEndPoint request)
	{
		HttpServerRequest serverRequest = (HttpServerRequest) request;
		
		HttpServletResponse response = serverRequest.getResponse();
		try
		{
			response.flushBuffer();
			response.getOutputStream().close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
